package com.bee.user.controller.web;

import com.bee.commons.utils.StringUtil;
import com.bee.commons.vo.Result;
import com.bee.commons.web.constrants.WebConst;
import com.bee.user.common.domain.OrgDO;
import com.bee.user.common.vo.OrgVO;
import com.bee.user.service.OrgService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 机构表(Org)表控制层
 *
 * @author makejava
 * @since 2019-08-27 10:19:14
 */
@Slf4j
@Api(tags = "机构管理模块")
@RestController
@RequestMapping(WebConst.MANAGEMENT + "/org")
public class OrgController {

    @Autowired
    private OrgService orgService;


    @ApiOperation(value = "分页查询数据")
    @GetMapping("/getList")
    public Result<PageInfo<OrgVO>> getList(
            @ApiParam(value = "名称") @RequestParam(required = false) String name,
            @ApiParam(value = "页码", defaultValue = "1") @RequestParam(defaultValue = "1") int pageNo,
            @ApiParam(value = "条数", defaultValue = "20") @RequestParam(defaultValue = "20") int pageSize
    ) {

        OrgVO orgVO = new OrgVO();
        if (StringUtil.isNotEmpty(name)) {
            orgVO.setOrgName(name);
        }
        PageInfo<OrgVO> pageInfo = orgService.queryPage(orgVO, pageNo, pageSize);
        return Result.success(pageInfo);
    }

    @ApiOperation(value = "查询有效数据")
    @GetMapping("/queryAllEnable")
    public Result<List<OrgDO>> getEnable() {
        return Result.success(orgService.queryAllEnable());
    }

    @ApiOperation(value = "查询单条数据")
    @GetMapping()
    public Result<OrgDO> get(
            @ApiParam(value = "id主键") @RequestParam Long id
    ) {
        OrgDO domain = orgService.queryById(id);
        return Result.success(domain);
    }

    @ApiOperation(value = "添加")
    @PostMapping()
    public Result<?> add(OrgDO domain) {
        orgService.insert(domain);
        return Result.success(null);
    }

    @ApiOperation(value = "修改")
    @PutMapping()
    public Result<?> update(OrgDO domain) {
        orgService.update(domain);
        return Result.success(null);
    }

    @ApiOperation(value = "删除")
    @DeleteMapping()
    public Result<?> delete(Long id) {
        if (orgService.deleteById(id)) {
            return Result.success(null);
        }
        return Result.fail("删除失败");
    }
}